package com.fas.system.service.impl;

import com.fas.common.core.domain.AjaxResult;
import com.fas.system.domain.FasFacilitiesType;
import com.fas.system.domain.FasFacilitiesTypeDataItem;
import com.fas.system.mapper.FasFacilitiesMapper;
import com.fas.system.mapper.FasFacilitiesTypeMapper;
import com.fas.system.service.IFasFacilitiesTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FasFacilitiesTypeServiceImpl implements IFasFacilitiesTypeService {

    @Resource
    private FasFacilitiesTypeMapper fasFacilitiesTypeMapper;
    @Resource
    private FasFacilitiesMapper fasFacilitiesMapper;

    @Override
    public List<FasFacilitiesType> findList(FasFacilitiesType fasFacilitiesType) {
        List<FasFacilitiesType> list = fasFacilitiesTypeMapper.findList(fasFacilitiesType);

        for (FasFacilitiesType facilitiesType : list) {
            facilitiesType.setSpec_num(facilitiesType.getFasFacilitiesTypeSpecList().size());
            List<FasFacilitiesTypeDataItem> dataItemList = facilitiesType.getFasFacilitiesTypeDataItemList();
            if (dataItemList != null && dataItemList.size() > 0) {
                int inspect_num = 0;
                int check_num = 0;
                for (FasFacilitiesTypeDataItem dataItem : dataItemList) {
                    // 数据项类型（1：检查项，2：检测项）
                    if (dataItem.getDataType() == 1) {
                        inspect_num += 1;
                    } else {
                        check_num += 1;
                    }
                }
                facilitiesType.setInspect_num(inspect_num);
                facilitiesType.setCheck_num((check_num));
            }
        }

        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addFasFacilitiesType(FasFacilitiesType fasFacilitiesType) {
        return fasFacilitiesTypeMapper.addFasFacilitiesType(fasFacilitiesType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateFasFacilitiesType(FasFacilitiesType fasFacilitiesType) {
        return fasFacilitiesTypeMapper.updateFasFacilitiesType(fasFacilitiesType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteFasFacilitiesType(Integer id) {
        int exists = fasFacilitiesMapper.existsFacilitiesType(id);
        if (exists > 0) {
            return AjaxResult.error("该设施类型有设施使用，无法删除!");
        }
        int count = fasFacilitiesTypeMapper.deleteFasFacilitiesType(id);
        return count > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchUpdateDelFlag(List<Integer> ids, String delFlag) {
        return fasFacilitiesTypeMapper.batchUpdateDelFlag(ids, delFlag);
    }

}
